package org.dx.oho.controller;

import cn.hutool.json.JSONUtil;
import dx.util.JsonUtil;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 接收推送的数据
 * create by duanx 2021/12/27
 *@description： 模拟大屏接口
 *@classname: ExternalDataController
 *@author: duanx
 *@date: 2021/12/27 10:12
 **/
@Controller
@RequestMapping(value = "/arcpscreen/open/api/screen/ExternalData/")
public class ExternalDataController {

    private Logger logger = LoggerFactory.getLogger(ExternalDataController.class);

    @NotNull
    private Map<String, Object> dataDeal(@NotNull String jsonStr){
        Map<String ,Object> modelMap = new HashMap<>(8);
        if (jsonStr.isEmpty()){
            modelMap.put("success",false);
            modelMap.put("code","-1");
            return modelMap;
        }
        logger.info("\n"+JSONUtil.toJsonPrettyStr(jsonStr));
        modelMap.put("success",true);
        modelMap.put("code","0");
        Map map = JSONUtil.toBean(jsonStr,Map.class);
        modelMap.putAll(map);
        return modelMap;
    }



    @PostMapping("insert_01_CityRelatedTotalNum")
    @ResponseBody
    public Map<String, Object> cityRelatedTotalNum(@NotNull @RequestBody String jsonStr){
        logger.info("insert_01_CityRelatedTotalNum====");
        return dataDeal(jsonStr);
    }


    @PostMapping("insert_02_CityRelatedDayNum")
    @ResponseBody
    public Map<String, Object> cityRelatedDayNum(@NotNull @RequestBody String jsonStr){
        logger.info("insert_02_CityRelatedDayNum====");
        return dataDeal(jsonStr);
    }

    @PostMapping("insert_03_CityHourNum")
    @ResponseBody
    public Map<String, Object> cityHourNum(@NotNull @RequestBody String jsonStr){
        logger.info("insert_03_CityHourNum====");
        return dataDeal(jsonStr);
    }

    @PostMapping("insert_04_CityHourSumNum")
    @ResponseBody
    public Map<String, Object> cityHourSumNum(@NotNull @RequestBody String jsonStr){
        logger.info("insert_04_CityHourSumNum====");
        return dataDeal(jsonStr);
    }

    @PostMapping("insert_05_DayCollectAddressNum")
    @ResponseBody
    public Map<String, Object> dayCollectAddressNum(@NotNull @RequestBody String jsonStr){
        logger.info("insert_05_DayCollectAddressNum====");
        return dataDeal(jsonStr);
    }

    @PostMapping("insert_06_DayAreaRelatedTotalNum")
    @ResponseBody
    public Map<String, Object> dayAreaRelatedTotalNum(@NotNull @RequestBody String jsonStr){
        logger.info("insert_06_DayAreaRelatedTotalNum====");
        return dataDeal(jsonStr);
    }

    @Scheduled(cron = "0 0 0/1 * * ?")
    @PostMapping("insert_07")
    @ResponseBody
    public void test01(){
        logger.info("NetUtil.getLocalhost() 推送数据07：");
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("/")).getPath()+"/neet.json";
        String json = JsonUtil.readJsonFile(filePath);
        assert json != null;
        dataDeal(json);
    }

    @Scheduled(cron = "0 0 0/1 * * ?")
    @PostMapping("insert_08")
    @ResponseBody
    public void test02(){
        logger.info("NetUtil.getLocalhost() 推送数据08：");
        String filePath = Objects.requireNonNull(this.getClass().getClassLoader().getResource("/")).getPath()+"/neet.json";
        String json = JsonUtil.readJsonFile(filePath);
        assert json != null;
        dataDeal(json);
    }


}
